/*
 * pps_bom
 */
package com.pitop.project.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.BomException;
import com.pitop.core.exceptions.ProjectCategoryException;
import com.pitop.project.dao.ProjectCategoryDAO;
import com.pitop.project.entity.ProjectCategory;
import com.pitop.project.entity.ProjectCategoryState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("projectCategorySV")
@Slf4j
public class ProjectCategorySVImpl extends BaseSVImpl<ProjectCategory, Long> implements ProjectCategorySV {

    @Autowired
    private ProjectCategoryDAO projectCategoryDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return projectCategoryDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(ProjectCategory entity) throws BaseException {
        //参数合法验证
        if (entity == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringUtils.isBlank(entity.getName())){
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringUtils.isBlank(entity.getEnglishName())){
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //检验名称是否重复
        Map<String,Object> map = new HashMap<>();
        map.put("name",entity.getName());
        ProjectCategory category = load(map);
        if (category != null) {
            throw new BomException(BaseException.BaseExceptionEnum.Exists);
        }
        //检验英文名是否重复
        map.clear();
        map.put("englishName",entity.getEnglishName());
        category = load(map);
        if (category != null) {
            throw new BomException(BaseException.BaseExceptionEnum.Exists);
        }
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象ProjectCategory
     *
     * @param id   id
     * @param code 编码
     * @return ProjectCategory
     */
    @Override
    public ProjectCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return projectCategoryDAO.load(param);
    }


    /**
     * 加载一个对象ProjectCategory 通过id
     *
     * @param id id
     * @return ProjectCategory
     */
    @Override
    public ProjectCategory loadById(Long id) {
        if (id == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectCategoryDAO.loadById(id);
    }

    /**
     * 加载一个对象ProjectCategory 通过code
     *
     * @param code 编码
     * @return ProjectCategory
     */
    @Override
    public ProjectCategory loadByCode(String code) {
        if (code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectCategoryDAO.loadByCode(code);
    }

    /**
     * 加载一个对象ProjectCategory 通过name
     *
     * @param name 项目分类名称
     * @return ProjectCategory
     */
    @Override
    public ProjectCategory loadByName(String name) {
        return projectCategoryDAO.loadByName(name);
    }


    /**
     * 根据主键id,oldStates 共同更新 ProjectCategory 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ProjectCategoryState newState, ProjectCategoryState... oldStates) {
        if (id == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectCategoryDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 ProjectCategory 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, ProjectCategoryState newState, ProjectCategoryState... oldStates) {
        if (code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectCategoryDAO.updateStateByCode(code, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 ProjectCategory 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ProjectCategoryState state) {
        if (id == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectCategoryDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键code 更新 ProjectCategory 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, ProjectCategoryState state) {
        if (code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectCategoryDAO.updateByCode(code, state.name(), new Date());
    }


    /**
     * 删除对象ProjectCategory
     *
     * @param id id* @param code 编码
     * @return ProjectCategory
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        projectCategoryDAO.delete(param);
    }


    /**
     * 查询ProjectCategory分页
     *
     * @param projectCategory 对象
     * @param offset          查询开始行
     * @param limit           查询行数
     * @return List<ProjectCategory>
     */
    @Override
    public List<ProjectCategory> list(ProjectCategory projectCategory, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (projectCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(projectCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectCategoryDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ProjectCategory projectCategory) {
        Map<String, Object> map = null;
        if (projectCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(projectCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectCategoryDAO.count(map);
    }

    /**
     * 查询ProjectCategory分页
     *
     * @param id     id* @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ProjectCategory>
     */
    @Override
    public List<ProjectCategory> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return projectCategoryDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return projectCategoryDAO.count(param);
    }

    @Override
    public void modify(ProjectCategory entity) throws BaseException {
        if (entity == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (entity.getId() == null){
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if(StringUtils.isBlank(entity.getName()) && StringUtils.isBlank(entity.getEnglishName())){
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectCategoryDAO.update(entity);
    }
}
